﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JXT.PrimaryKey.Batman.Cache
{
    /// <summary>
    /// 系统通用缓存基类
    /// </summary>
    public abstract class CacheBase : ICache
    {
        private static readonly TimeSpan _defaultTimeSpan = TimeSpan.FromMinutes(15); //默认过期时间
        private static readonly string _defaultNamespace = "_default"; //默认命名空间

        /// <summary>
        /// 增加新的缓存项目
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存对象</param>
        public virtual void Add(string key, object value)
        {
            Add(key, value, _defaultTimeSpan);
        }

        /// <summary>
        /// 增加新的缓存项目
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存对象</param>
        /// <param name="ts">过期时间</param>
        public virtual void Add(string key, object value, TimeSpan ts)
        {
            Add(_defaultNamespace, key, value, ts);
        }

        /// <summary>
        /// 增加新的缓存项
        /// </summary>
        /// <param name="ns">命名空间</param>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存对象</param>
        public virtual void Add(string ns, string key, object value)
        {
            Add(ns, key, value, _defaultTimeSpan);
        }

        /// <summary>
        /// 增加新的缓存项
        /// </summary>
        /// <param name="ns">命名空间</param>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存对象</param>
        /// <param name="ts">过期时间</param>
        public abstract void Add(string ns, string key, object value, TimeSpan ts);

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns>获取的缓存对象</returns>
        public virtual object Get(string key)
        {
            return Get(_defaultNamespace, key);
        }

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="ns">命名空间</param>
        /// <param name="key">缓存key</param>
        /// <returns>获取的缓存对象</returns>
        public abstract object Get(string ns, string key);

        /// <summary>
        /// 检测缓存对象是否存在
        /// </summary>
        /// <param name="ns">命名空间</param>
        /// <param name="key">缓存key</param>
        /// <returns>检查结果</returns>
        public abstract bool Exists(string ns, string key);

        /// <summary>
        /// 检测缓存对象是否存在
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns>检查结果</returns>
        public virtual bool Exists(string key)
        {
            return Exists(_defaultNamespace, key);
        }

        /// <summary>
        /// 删除缓存对象
        /// </summary>
        /// <param name="key">缓存key</param>
        public virtual void Remove(string key)
        {
            Remove(_defaultNamespace, key);
        }

        /// <summary>
        /// 删除缓存对象
        /// </summary>
        /// <param name="ns">命名空间</param>
        /// <param name="key">缓存key</param>
        public abstract void Remove(string ns, string key);

        /// <summary>
        /// 根据命名空间清空缓存
        /// </summary>
        /// <param name="ns">命名空间</param>
        public abstract void Clear(string ns);

        /// <summary>
        /// 清除系统中的所有缓存
        /// </summary>
        public abstract void Clear();
    }
}
